package com.xbongbong.formula.qlexpress.operator.math;

import com.ql.util.express.Operator;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;

import java.math.BigDecimal;

/**
 * @author: wufeng
 * @date: 2018/7/4 10:29
 * @desrcption: 计算abs，返回绝对值
 */
public class OperatorAbs extends Operator {


    public OperatorAbs(String name){
        this.name = name;
    }

    public OperatorAbs(String aAliasName, String aName, String errorInfo){
        this.aliasName = aAliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }
    @Override
    public Object executeInner(Object[] list) throws Exception {
        if (list[0] instanceof Float) {
            return Math.abs((Float) list[0]);
        } else if(list[0] instanceof Double) {
            return Math.abs((Double)list[0]);
        } else if(list[0] instanceof Long) {
            return Math.abs((Long)list[0]);
        } else if (list[0] instanceof Integer) {
            return Math.abs((Integer) list[0]);
        } else if (list[0] instanceof BigDecimal) {
            BigDecimal bigDecimal = new BigDecimal(list[0].toString());
            return bigDecimal.abs();
        } else {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260017);
        }
    }
}
